package com.jinzhi.jzweb.controller;


import java.util.*;

import cn.hutool.core.date.DateTime;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.domain.DetailsReportDO;
import com.jinzhi.jzweb.service.DetailsReportService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.TopicReportDO;
import com.jinzhi.jzweb.service.TopicReportService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2023-04-04 17:59:17 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/topicReport")
public class TopicReportController extends BaseController {
	@Autowired
	private UserService userService;
	@Autowired
	private DetailsReportService detailsReportService;
	@Autowired
	private TopicReportService topicReportService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:topicReport:topicReport")
	String TopicReport(){
	    return "jzweb/topicReport/topicReport";
	}

	@Log("查询专题报道列表信息")
	@ResponseBody
	@GetMapping("/topicReportList")
	@RequiresPermissions("jzweb:topicReport:topicReport")
	public Result<Page<TopicReportDO>> topicReportList(@RequestParam("name")String topicName){
		Wrapper<TopicReportDO> wrapper = new EntityWrapper<TopicReportDO>().orderBy("sort_order",true).orderBy("id", false);
		if (StringUtil.notEmpty(topicName)) {
			wrapper.like("topic_name", topicName);
		}
		// 查询列表数据
		Page<TopicReportDO> page = topicReportService.selectPage(getPage(TopicReportDO.class),wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			TopicReportDO obj = (TopicReportDO) page.getRecords().get(i);
			UserDO userDO = userService.selectById(obj.getUserId());
			obj.setUserName(userDO.getUsername());
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}

	@Log("进入新建专题页面")
	@GetMapping("/add")
	@RequiresPermissions("jzweb:topicReport:add")
	String add(){
	    return "jzweb/topicReport/add";
	}

	@Log("新建并保存专题")
	@ResponseBody
	@PostMapping("/saveTopicReport")
	@RequiresPermissions("jzweb:topicReport:add")
	public Result<String> save( TopicReportDO topicReport){
		topicReport.setCreatedDate(new DateTime());
		topicReport.setUserId(getUser().getId());
		topicReport.setUserName(getUser().getUsername());
		topicReportService.insert(topicReport);
		return Result.ok();
	}

	@Log("进入专题报道详情")
	@GetMapping("/topicReportDetail/{id}")
	@RequiresPermissions("jzweb:topicReport:topicReport")
	String topicReportDetail(@PathVariable("id") String id, Model model) {
		TopicReportDO topicReportDO = topicReportService.selectById(Long.parseLong(id));
		topicReportDO.setUserName(userService.selectById(topicReportDO.getUserId()).getUsername());
		topicReportDO.setCreatedDate(DateTime.of(topicReportDO.getCreatedDate()));
		topicReportDO.setUpdateDate(DateTime.of(topicReportDO.getUpdateDate()));
		model.addAttribute("topicReportDO", topicReportDO);
		return "jzweb/topicReport/topicReportDetail";
	}

	@Log("进入专题编辑页面")
	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:topicReport:edit")
	String edit(@PathVariable("id") Long id,Model model){
		TopicReportDO topicReport = topicReportService.selectById(id);
		topicReport.setCreatedDate(DateTime.of(topicReport.getCreatedDate()));
		topicReport.setUpdateDate(DateTime.of(topicReport.getUpdateDate()));
		model.addAttribute("topicReport", topicReport);
		return "jzweb/topicReport/edit";
	}

	@Log("修改并更新专题数据")
	@ResponseBody
	@RequestMapping("/updateTopicReport")
	@RequiresPermissions("jzweb:topicReport:edit")
	public Result<String>  update( TopicReportDO topicReport){
		topicReport.setUpdateDate(new DateTime());
		boolean update = topicReportService.updateById(topicReport);
		return update ? Result.ok() : Result.fail();
	}

	@Log("删除单个专题")
	@PostMapping( "/removeTopicReport")
	@ResponseBody
	@RequiresPermissions("jzweb:topicReport:remove")
	public Result<String>  removeTopicReport( Long id){
		topicReportService.deleteById(id);
		return Result.ok();
	}

	@Log("批量删除专题")
	@PostMapping( "/batchRemoveTopicReport")
	@ResponseBody
	@RequiresPermissions("jzweb:topicReport:batchRemove")
	public Result<String>  batchRemoveTopicReport(@RequestParam("ids[]") Long[] ids){
		topicReportService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
